#include "SegmentationDialog.h"

SegmentationDialog::SegmentationDialog(QWidget *parent /* = 0 */):QDialog(parent)
{
	isOKButtonClicked = false;
	this->resize(430, 165);
	this->setMinimumSize(QSize(430, 165));
	this->setMaximumSize(QSize(430, 165));
	buttonBox = new QDialogButtonBox(this);
	buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
	buttonBox->setGeometry(QRect(130, 120, 161, 32));
	buttonBox->setOrientation(Qt::Horizontal);
	buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
	label = new QLabel(this);
	label->setObjectName(QString::fromUtf8("label"));
	label->setGeometry(QRect(20, 60, 111, 16));
	label_2 = new QLabel(this);
	label_2->setObjectName(QString::fromUtf8("label_2"));
	label_2->setGeometry(QRect(20, 90, 111, 16));
	lineEdit_ProjectLocation = new QLineEdit(this);
	lineEdit_ProjectLocation->setObjectName(QString::fromUtf8("lineEdit_ProjectLocation"));
	lineEdit_ProjectLocation->setGeometry(QRect(140, 60, 200, 20));
	lineEdit_ImagePath = new QLineEdit(this);
	lineEdit_ImagePath->setObjectName(QString::fromUtf8("lineEdit_ImagePath"));
	lineEdit_ImagePath->setGeometry(QRect(140, 90, 200, 20));
	toolButton_1 = new QToolButton(this);
	toolButton_1->setObjectName(QString::fromUtf8("toolButton_1"));
	toolButton_1->setGeometry(QRect(350, 60, 71, 21));
	toolButton_2 = new QToolButton(this);
	toolButton_2->setObjectName(QString::fromUtf8("toolButton_2"));
	toolButton_2->setGeometry(QRect(350, 90, 71, 21));
	lineEdit_ProjectName = new QLineEdit(this);
	lineEdit_ProjectName->setObjectName(QString::fromUtf8("lineEdit_ProjectName"));
	lineEdit_ProjectName->setGeometry(QRect(140, 30, 200, 20));
	label_3 = new QLabel(this);
	label_3->setObjectName(QString::fromUtf8("label_3"));
	label_3->setGeometry(QRect(20, 30, 111, 16));


	this->setWindowTitle(QApplication::translate("Dialog", "Segmentation Project Settings", 0, QApplication::UnicodeUTF8));
	label->setText(QApplication::translate("Dialog", "Project Location\357\274\232", 0, QApplication::UnicodeUTF8));
	label_2->setText(QApplication::translate("Dialog", "Choose An  Image\357\274\232", 0, QApplication::UnicodeUTF8));
	toolButton_1->setText(QApplication::translate("Dialog", "Browse...", 0, QApplication::UnicodeUTF8));
	toolButton_2->setText(QApplication::translate("Dialog", "Browse...", 0, QApplication::UnicodeUTF8));
	label_3->setText(QApplication::translate("Dialog", "Project Name\357\274\232", 0, QApplication::UnicodeUTF8));

	QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(OKButtonClicked()));
	QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(CancelButtonClicked()));

	QObject::connect(toolButton_1, SIGNAL(clicked()), this, SLOT(setExistingDirectory()));
	QObject::connect(toolButton_2, SIGNAL(clicked()), this, SLOT(setOpenFileName()));
}

SegmentationDialog::~SegmentationDialog()
{

}
void SegmentationDialog::setExistingDirectory()
{
	QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
	QString directory = QFileDialog::getExistingDirectory(this,
		tr("Segmentation Project Location"),
		lineEdit_ProjectLocation->text(),
		options);
	if (!directory.isEmpty())
		lineEdit_ProjectLocation->setText(directory);
}
void SegmentationDialog::setOpenFileName()
{
	QFileDialog::Options options;
	QString selectedFilter;
	QString fileName = QFileDialog::getOpenFileName(this,
		tr("QFileDialog::getOpenFileName()"),
		lineEdit_ImagePath->text(),
		tr("Image Files (*.png *.jpg *.bmp)"),
		&selectedFilter,
		options);
	if (!fileName.isEmpty())
		lineEdit_ImagePath->setText(fileName);
}
void SegmentationDialog::OKButtonClicked()
{
	while(lineEdit_ProjectName->text() == NULL||lineEdit_ImagePath->text() == NULL||
		lineEdit_ProjectLocation->text() == NULL)
	{
		QMessageBox::about(0,"ERROR!","The project name and locaton and the image path cannot be empty!");
		return;
	}
	
	QImage image;
	if(image.load(lineEdit_ImagePath->text())==false)
	{
		QMessageBox::about(0,"ERROR!","The image path is not effective!");
		return;
	}
	QDir dir(lineEdit_ProjectLocation->text());
	if (!dir.exists())
	{
		QMessageBox::about(0,"ERROR!","The project path is not effective!");
		return;
	}	
	projectName=lineEdit_ProjectName->text();
	if(dir.mkdir(projectName)==false)
	{
		QMessageBox::about(0,"ERROR!","The project is created error!");
		return;
	}
	projectFullPath=lineEdit_ProjectLocation->text();
	projectFullPath.append('/');
	projectFullPath.append(projectName);
	imagePath=projectFullPath;
	imagePath.append('/');
	imagePath.append(lineEdit_ImagePath->text().section('/', -1));

	if(QFile::copy(lineEdit_ImagePath->text(), imagePath)==false)
	{
		QMessageBox::about(0,"ERROR!","The image is copied error!");
		dir.rmdir(projectName);
		return;
	}
	isOKButtonClicked = true;
	accept();
}
void SegmentationDialog::CancelButtonClicked()
{
	isOKButtonClicked = false;
	reject();
}

SegmentationOpenDialog::SegmentationOpenDialog(QWidget *parent):QDialog(parent)
{

	resize(373, 134);
	isOKButtonClicked = false;
	this->setMinimumSize(QSize(373, 134));
	this->setMaximumSize(QSize(373, 134));
	label = new QLabel(this);
	label->setObjectName(QString::fromUtf8("label"));
	label->setGeometry(QRect(20, 20, 91, 16));
	lineEdit = new QLineEdit(this);
	lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
	lineEdit->setGeometry(QRect(20, 50, 261, 20));
	toolButton = new QToolButton(this);
	toolButton->setObjectName(QString::fromUtf8("toolButton"));
	toolButton->setGeometry(QRect(290, 50, 60, 21));
	buttonBox = new QDialogButtonBox(this);
	buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
	buttonBox->setGeometry(QRect(110, 90, 156, 23));
	buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);

	setWindowTitle(QApplication::translate("SegmentationOpenDialog", "Segmentation Open", 0, QApplication::UnicodeUTF8));
	label->setText(QApplication::translate("SegmentationOpenDialog", "Project Path\357\274\232", 0, QApplication::UnicodeUTF8));
	toolButton->setText(QApplication::translate("SegmentationOpenDialog", "Browse...", 0, QApplication::UnicodeUTF8));

	QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(OKButtonClicked()));
	QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(CancelButtonClicked()));
	QObject::connect(toolButton, SIGNAL(clicked()), this, SLOT(setExistingDirectory()));
}
SegmentationOpenDialog::~SegmentationOpenDialog()
{

}
void SegmentationOpenDialog::setExistingDirectory()
{
	QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
	QString directory = QFileDialog::getExistingDirectory(this,
		tr("Choose Project to Open"),
		lineEdit->text(),
		options);
	if (!directory.isEmpty())
		lineEdit->setText(directory);
}

void SegmentationOpenDialog::OKButtonClicked()
{
	while(lineEdit->text()== NULL)
	{
		QMessageBox::about(0,"ERROR!","Please choose a segmentation projec!");
		return;
	}

	
	QDir dir(lineEdit->text());
	if (!dir.exists()) 
	{
		QMessageBox::about(0,"ERROR!","The project path is not effective!");
		return;
	}	
	projectFullPath=dir.absolutePath();
/*
	projectName=projectFullPath.section('/',-1);
	if(!dir.exists(projectName+QString(".log")))
	{
		QMessageBox::about(0,"ERROR!","The project log file cannot be found!");
		return;
	}
	logFilePath=projectFullPath+QString("/")+projectName+QString(".log");*/

 	
 	dir.setFilter(QDir::Files);
 	dir.setSorting(QDir::Type);
 
 	QFileInfoList fileList = dir.entryInfoList();
 	if(fileList.size()<2)
 	{
		QMessageBox::about(0,"ERROR!","The project log file or the image cannot be found!");
		return; 
 	}
 	bool log=false;
 	bool image=false;
 	for (int i = 0; i < fileList.size(); ++i) 
	{
		QFileInfo fileInfo = fileList.at(i);
		if(fileInfo.isFile())
		{
			QString ext = fileInfo.suffix();
			if(ext==QString("log"))
			{
				log=true;
				logFilePath=fileInfo.absoluteFilePath();
			}
			else if(ext==QString("png")||ext==QString("jpg")||ext==QString("bmp"))
			{
				image=true;
			}
		}		
 	}


if(log==false)
{
	QMessageBox::about(0,"ERROR!","The project log file cannot be found!");
	return;

}
if(image==false)
{
	QMessageBox::about(0,"ERROR!","The image file cannot be found!");
	return;

}
	isOKButtonClicked = true;
	accept();
}
void SegmentationOpenDialog::CancelButtonClicked()
{
	isOKButtonClicked = false;
	reject();
}
